package com.lhh.sysadmin.common.security;

import com.lhh.sysadmin.common.exception.UserCountLockException;
import com.lhh.sysadmin.entity.SysUser;
import com.lhh.sysadmin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义UserDetails
 * @author lhh
 * @Date 2024-09-18-38 17:38
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser sysUSer = sysUserService.getByUsername(s);
        if (sysUSer == null){
            throw new UsernameNotFoundException("用户名或者密码错误！");
        }else if ("1".equals(sysUSer.getStatus()))
        {
            throw new UserCountLockException("该用户账号被封禁，具体清联系管理员！");
        }

        return new User(sysUSer.getUsername(),sysUSer.getPassword(),getUserAuthority());
    }

    public List<GrantedAuthority> getUserAuthority()
    {
        return new ArrayList<>();
    }
}
